﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiBoost.Core.CustomProperties.EnumSelector;
using EPiServer.Shell.ObjectEditing;
using EPiServer.Shell.ObjectEditing.EditorDescriptors;
using EPiServer.Web.Mvc.VisitorGroups;

namespace EPiBoost.Core.CustomProperties.EditorDescriptors
{
	[EditorDescriptorRegistration(TargetType = typeof(string), UIHint = "Enum")]
  public class EnumEditorDescriptor : EditorDescriptor
  {
      public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
      {
          SelectionFactoryType = typeof(EnumSelectionFactory);
          ClientEditingClass = "epi.cms.contentediting.editors.SelectionEditor";
          base.ModifyMetadata(metadata, attributes);
      }
  }
	[EditorDescriptorRegistration(TargetType = typeof(string), UIHint = "EnumMulti")]
	public class EnumMultiEditorDescriptor : EditorDescriptor
  {
      public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
      {
				SelectionFactoryType = typeof(EnumSelectionFactory);
				ClientEditingClass = "epi.cms.contentediting.editors.CheckBoxListEditor";
          base.ModifyMetadata(metadata, attributes);
      }
  }

	public class EnumSelectionFactory : ISelectionFactory
	{
		public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
		{
			var options = EnumHelper.GetEnumCollectionFromEnum<ExampleEnum>(true);

			var enumValues = new List<SelectItem>();
			foreach (var option in options)
			{
				var item = new SelectItem() {Value = option.Value.ToString(), Text = option.Value.ToString()};
				if(option.Attributes!=null)
				{
					if (!option.Attributes.Hidden && !option.Attributes.Removed)
					{
						if(!string.IsNullOrWhiteSpace(option.Attributes.DisplayName))
						{
							item.Text = option.Attributes.DisplayName;
						}
						enumValues.Add(item);
					}
				}
				else
				{
					enumValues.Add(item);
				}
				
			}

			return enumValues;
		}
		 

	}


	public enum ExampleEnum
	{
		[EnumSelectorOption(DisplayName = "One!", Order = 1, Hidden = true)]
		One,
		[EnumSelectorOption(DisplayName = "Two-", Order = 2)]
		Two,
		[EnumSelectorOption(DisplayName = "Three:", Order = 3, Removed = true)]
		Three,
		[EnumSelectorOption(DisplayName = "Four$", Order = 4, IsDefault = true)]
		Four,
		[EnumSelectorOption(DisplayName = "Five*", Order = 5)]
		Five

	}

}
